<?php
/**
 * 周舟  hzboye010@163.com
 * sublime3 - home edited
 */

namespace Admin\Controller;

use Common\Api\ScoreGoodsApi;
// use Admin\Api\WxstoreApi;
// use Shop\Api\CategoryApi;
// use Shop\Api\ProductApi; 
// use Shop\Api\StoreApi; 
// use Shop\Api\StorePromotionApi;
// use Shop\Model\StorePromotionModel;

class ScoreGoodsController extends AdminController{
  
  protected function _initialize(){
    parent::_initialize();
  }

  public function index($p=1){
    // $page = I('get.page') && intval(I('get.page'))?I('get.page'):1;
    $r = apiCall(ScoreGoodsApi::ORPAGE,array(array(),array('curpage'=>$p,'size'=>10)));
    if($r['status'])
    {
        $list = $r['info']['list'];
        // foreach ($list as $k => $v) {
            // $list[$k]['detail'] = mb_substr(htmlspecialchars_decode($v['detail']),0,20);
        // }
        // dump($list);exit;
        $this->assign('list',$list);
        $this->assign('show',$r['info']['show']);
    }else{
        $this->error('ScoreGoodsApi:index-Page:ERROR');
    }
    $this->display();
  }

  public function delete($gid){
    if(IS_GET){
      $r = apiCall(ScoreGoodsApi::DELETE,array(array('id'=>$gid)));
      if($r['status'])
        $this->success('删除成功');
      else
        $this->error('删除失败');
    }
  }


  public function add(){
    if(IS_GET) $this->display();
    else if(IS_POST){
      $map = I('post.');
      if($map['main_img'] && intval($map['main_img'])){
        $map['main_img'] = intval($map['main_img']);
      }else{
        unset($map['main_img']);
      }
      $map['score']     = I('post.score',1,'intval');
      $map['price']     = number_format(I('post.price',0.00,'float'),2);
      $map['prize_cnt'] = I('post.prize_cnt',1,'intval');
      $map['sort']      = I('post.sort',0,'intval');

      // dump($map);
      $r = apiCall(ScoreGoodsApi::ADD,array($map));
      if($r['status'])
        $this->success('添加成功',U('ScoreGoods/index'));
      else
        $this->error('添加失败');
    }
  }
  public function edit($gid){
    // exit('sdsd');
    if(IS_GET){
      $r = apiCall(ScoreGoodsApi::GET_INFO,array(array('id'=>$gid)));
      // dump($r);exit;

      $this->assign('entry',$r['info']);
      $this->display();

    }else if(IS_POST){
      $map = I('post.');
      $map['score']     = I('post.score',1,'intval');
      $map['prize_cnt'] = I('post.prize_cnt',1,'intval');
      $map['sort']      = I('post.sort',0,'intval');
      $map['price']     = number_format(I('post.price',0.00,'float'),2);
      // echo $gid;
      // dump($map['price']);exit;
      $r = apiCall(ScoreGoodsApi::SAVE,array(array('id'=>$gid),$map));
      // dump($r);exit;
      if($r['status'])
        $this->success(L('RESULT_SUCCESS'),U('ScoreGoods/index'));
      else
        $this->error(L('RESULT_FAIL'));
    }
  }
  public function change($gid,$onshelf){
    $r = apiCall(ScoreGoodsApi::CHANGE,array($gid,$onshelf));
    // dump($r);exit;
    if($r['status'])
      $this->display('index');
    else
      $this->error('更改失败');
  }
  public function search($p=1){
    $kword = I('post.kword');
    // dump($kword);exit;
    // $page = I('get.page') && intval(I('get.page'))?I('get.page'):1;
    // if(IS_GET){
      $this->assignTitle($kword.' - 搜索结果');
      $where = array();
      $where['id'] = $kword;
      $where['name']   = array('like','%'.$kword.'%');
      $where['_logic'] = 'OR';
      $r = apiCall(ScoreGoodsApi::ORPAGE,array($where,array('curpage'=>$p,'size'=>10)));
      // dump($r->lastSql());
      if($r['status'])
      {
          $list = $r['info']['list'];
          // foreach ($list as $k => $v) {
          //     $list[$k]['desc'] = mb_substr(htmlspecialchars_decode($v['description']),0,20);
          // }
          $this->assign('list',$list);
          $this->assign('show',$r['info']['show']);
      }else{
          $this->error('ScoreGoodsApi:SEARCH:ERROR');
      }
      $this->display('index');
    // }
  }
}